package com.rx.mgt.controller;

import com.rx.mgt.domain.AdminUser;
import com.rx.mgt.domain.Spam;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.SpamService;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.util.IpUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zxj on 2017/5/27.
 */
@Controller
public class SpamController {

    @Autowired
    SysLogService sysLogService;

    @Autowired
    private SpamService spamService;
    @Autowired
    private DomainService domainService;

    @RequestMapping("/spam-add.htmlx")
    private String spamInitpage(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        UsersBo usersBo = new UsersBo();
        List list = domainService.getDomainInfoExt(usersBo);
        String type = user.getType();
        if("2".equals(type)){
            request.setAttribute("dom",user.getDomain());
        }
        request.setAttribute("domainList",list);
        return "spam/spam-add";
    }
    @RequestMapping("ajaxGetSpamByDomain.htmlx")
    @ResponseBody
    private Map<String, Object> ajaxGetSpamByDomain(@RequestParam("domain")String domain){
        Map<String,Object> rs = new HashMap<String,Object>();
        Spam spam = spamService.getSpamByDomain(domain);
        rs.put("code","001");
        rs.put("obj",spam);
        return rs;
    }
    /**
     * 增加或修改系统反垃圾设置
     */
    @RequestMapping("/SpamSaveOrUpdate.htmlx")
    @ResponseBody
    public Map<String,Object> spamSaveOrUpdate(Spam spam,HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        Map<String,Object> rs = new HashMap<String, Object>();
        sysLogService.add("增加或修改系统反垃圾设置", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        if(spamService.saveOrUpdate(spam)>0){
            rs.put("code","001");
        }else{
            rs.put("code","002");
        }
        return rs;
    }
}
